﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ThreadEyePro
{
    public class KaskusThreadObject
    {
        private int _index = 0;
        private int _id = 0;
        private string _title = string.Empty;
        private int _postCount = 0;
        private int _pageNo = 1;
        private int _pageCount = 1;
        private bool _hasNewPost = false;
        private string _status = string.Empty;

        public int index
        {
            get { return _index; }
            set { _index = value; }
        }

        public int threadId
        {
            get { return _id; }
            set { _id = value; }
        }

        public string threadTitle
        {
            get { return _title; }
            set { _title = value; }
        }

        public string threadTitleSafe
        {
            get
            {
                if (_title.Length > 60)
                {
                    return _title.Substring(0, 60) + "...";
                }
                return _title;
            }
            set { _title = value; }
        }

        public string status
        {
            get { return _status; }
            set { _status = value; }
        }

        public bool hasNewPost
        {
            get { return _hasNewPost; }
            set { _hasNewPost = value; }
        }

        public int postCount
        {
            get { return _postCount; }
            set { _postCount = value; }
        }

        public int pageNo
        {
            get { return _pageNo; }
            set { _pageNo = value; }
        }

        public int pageCount
        {
            get { return _pageCount; }
            set { _pageCount = value; }
        }

        public int realPageNo
        {
            get
            {
                return Convert.ToInt32(Math.Ceiling((double)this.totalPostCount / 20));
            }
        }

        public int totalPostCount
        {
            get
            {
                int total = 1;
                if (_pageCount > 1)
                {
                    total = 10 * _pageCount;
                    if (_postCount < 10)
                    {
                        total = total - (10 - _postCount);
                    }
                }
                else
                {
                    total = _postCount;
                }
                return total;
            }
        }

        public string threadUrlMobile
        {
            get
            {
                int p = (_pageCount - 1) * 10;
                if (p < 0)
                {
                    p = 0;
                }
                return "http://m.kaskus.us/thread/" + _id.ToString() + "/" + p.ToString();
            }
        }

        public string threadUrl
        {
            get
            {
                return "http://www.kaskus.us/showthread.php?t=" + _id.ToString() + "&page=" + this.realPageNo;
            }
        }

        public string threadUrlReplyMobile
        {
            get
            {
                return "http://m.kaskus.us/reply/" + _id.ToString();
            }
        }

        public string threadHash
        {
            get
            {
                return threadId.ToString() + ":" + pageNo.ToString() + ":" + postCount.ToString() + ":" + _pageCount.ToString();
            }
        }

        public KaskusThreadObject()
        {
        }

        public KaskusThreadObject(int threadId)
        {
            _id = threadId;
        }

        public KaskusThreadObject(string threadHash)
        {
            string[] hashs = Regex.Split(threadHash, ":");
            _id = Convert.ToInt32(hashs[0]);
            _pageNo = Convert.ToInt32(hashs[1]);
            _postCount = Convert.ToInt32(hashs[2]);
            _pageCount = Convert.ToInt32(hashs[3]);
        }
    }
}
